package demo1;//FileRandomRW.java
import java.io.*;
import java.util.*;

public class Main {
    public static int NUMBER = 3; // 员工数

    public static void main(String[] args) {
        Employee[] employee = new Employee[NUMBER];
        employee[0] = new Employee("张一", 21, 1000);
        employee[1] = new Employee("张二", 22, 2000);
        employee[2] = new Employee("张三", 23, 3000);
        try {
            RandomAccessFile inout = new RandomAccessFile("employee.dat", "rw");
            writeData(employee, inout);// 将员工数据保存至文件中。
            Employee employee3 = new Employee();// 读取第三个员工数据
            employee3.readDataPer(inout, 3);
            System.out.println("第三个记录");
            System.out.println(employee3);
            inout.close(); // 关闭流。
        } catch (IOException exception) {
            System.err.println("IOException");
        }
    }// 将数据写入输出流。

    static void writeData(Employee[] p, RandomAccessFile inout)
            throws IOException {
        for (int i = 0; i < p.length; i++){
            p[i].writeDataPer(inout);
        }
    }
}

class FixStringIO// 对固定长度字符串从文件读出、写入文件
{
    public static String readFixString(int size, DataInput in)
            throws IOException { // 读取固定长度的Unicode字符串。
        StringBuffer b = new StringBuffer(size);
        int i = 0;
        boolean more = true;
        while (more && i < size) {
            char ch = in.readChar();
            i++;
            if (ch == 0)
                more = false;
            else
                b.append(ch);
        }
        in.skipBytes(2 * (size - i));// 跳过剩余的字节
        return b.toString();
    }

    public static void writeFixString(String s, int size, DataOutput out)
            throws IOException { // 写入固定长度的Unicode字符串
        int i;
        for (i = 0; i < size; i++) {
            char ch = 0;
            if (i < s.length())
                ch = s.charAt(i);
            out.writeChar(ch);
        }
    }
}

class Employee// 员工类
{
    private String name;// 8个字节
    private int age; // 4个字节
    private double salary; // 8个字节
    public static final int NAME_LENGTH = 4; // 姓名长度
    public static final int RECORD_LENGTH = NAME_LENGTH * 2 + 4 + 8;

    public Employee() {
    }

    public Employee(String n, int a, double s) {
        name = n;
        age = a;
        salary = s;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public double getSalary() {
        return salary;
    }

    public String toString() {// 设置输出格式。
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }

    // 写入一条固定长度的记录到随机读取文件中。
    public void writeDataPer(RandomAccessFile out) throws IOException {
        FixStringIO.writeFixString(name, NAME_LENGTH, out);
        out.writeInt(age);
        out.writeDouble(salary);
    }

    // 从输入流随机读入指定位置的记录。
    public void readDataPer(RandomAccessFile in, int n) throws IOException {
        in.seek((n - 1) * RECORD_LENGTH);
        name = FixStringIO.readFixString(NAME_LENGTH,in);
        age = in.readInt();
        salary = in.readDouble();
    }
}